package com.wu.neihan.main;

import java.util.Date;
import java.util.UUID;

import com.wu.neihan.model.User;
import com.wu.util.ConMysql;
import com.wu.util.DateUtil;
import com.wu.util.HttpUtil;
import com.wu.util.JsonUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class UserMain {

	public static void main(String[] args) {
		for(int i=1000;i<5000;i++){
		caiUserInfo(String.valueOf(i));// 52393547001 6089394201
		caiPostsInfo(String.valueOf(i), "");
		// String
		// url="http://i.snssdk.com/neihan/video/playback/?video_id=d66ce810b1d14935bfb01157e211e5e4&quality=480p&line=0&is_gif=0.mp4";
		// String body = HttpUtil.sendGetIO(url);
		// System.out.println(body);
		}
	}

	// 采集用户信息
	public static void caiUserInfo(String id) {
		// 获取采集链接
		String url = "http://isub.snssdk.com/neihan/user/profile/v2/?user_id=" + id;

		// 读取内容封装user
		String body = HttpUtil.sendGet(url);
		JSONObject bodyJs = JsonUtil.stringToJson(JsonUtil.stringToJson(body).getString("data"));
		if (bodyJs.size() > 1) {
			User user = new User(bodyJs.getString("large_avatar_url"), bodyJs.getString("avatar_url"),
					bodyJs.getString("name"), bodyJs.getString("city"), bodyJs.getInt("followers"),
					bodyJs.getInt("followings"), bodyJs.getInt("point"), bodyJs.getInt("ugc_count"),
					bodyJs.getInt("comment_count"));

			// 存储采集
			// "created,email,last_login,mobile,password,status,username,name,avatar,updated,gender,role_id,source,active_email,comments,fans,favors,follows,posts,signature,region
			// "
			System.out.println(bodyJs.getLong("create_time"));

			String sql = "INSERT INTO `mto_users` VALUES (" + bodyJs.getString("user_id") + ",'"
					+ DateUtil.getDate(bodyJs.getString("create_time") + "000", null) + "', '"
					+ bodyJs.getString("user_id") + "@qq.com', " + "'" + DateUtil.getDate(new Date(), null)
					+ "', null, '8FLF2EE7LSBJ8LIM6IIL261F8', '0', '" + user.getName() + "', '"
					+ bodyJs.getString("screen_name") + "', '" + bodyJs.getString("large_avatar_url") + "', null, '"
					+ bodyJs.getString("gender") + "', null, '0', '0', '" + user.getPinglun() + "', '" + user.getFensi()
					+ "', '" + bodyJs.getString("repin_count") + "','" + user.getGuanzhu() + "', '" + user.getTougao()
					+ "', '" + bodyJs.getString("description") + "', '" + user.getRegion() + "');";
			System.out.println(sql);
			try {
				if (ConMysql.insertSql(sql) == 1) {
					System.out.println("插入会员成功：" + user.getName());
				} else {
					System.out.println("插入会员失败：" + user.getName());
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			System.out.println("会员信息不纯在：" + id);
		}
	}

	// 采集投稿信息
	public static void caiPostsInfo(String id, String max_time) {
		// 获取采集链接
		String url = "http://is.snssdk.com/2/essay/zone/user/posts/?screen_width=1080&user_id=" + id + "&max_time="
				+ max_time;

		// 读取内容封装
		String body = HttpUtil.sendGet(url);
		JSONObject bodyJs = JsonUtil.stringToJson(JsonUtil.stringToJson(body).getString("data"));
		if (bodyJs.size() > 1) {
			JSONArray bodyJa = JsonUtil.stringToJsonArry(bodyJs.getString("data"));
			for (int i = 0; i < bodyJa.size(); i++) {

				JSONObject bodyTemp;
				bodyTemp = JsonUtil.stringToJson(JsonUtil.stringToJson(bodyJa.get(i).toString()).getString("group"));

				// 通用内容采集
				String group = "1";
				max_time = bodyTemp.getString("create_time");
				String biaoqian = bodyTemp.getString("category_name");
				String idstr = bodyTemp.getString("id");
				String statusDesc = bodyTemp.getString("status_desc");
				String text = bodyTemp.getString("text");
				String content = bodyTemp.getString("content");
				int digg = bodyTemp.getInt("digg_count");
				int bury = bodyTemp.getInt("bury_count");
				int share = bodyTemp.getInt("share_count");
				int comment = bodyTemp.getInt("comment_count");
				String imageUrl = "";
				String videoImgUrl = "";
				String videoUrl = "";
				String width = "";
				String height = "";
				String privacy = "0";// 是否私密，后面将改为是否匿名
				System.out.println(bodyTemp.toString());
				if (bodyTemp.getBoolean("is_anonymous")) {
					privacy = "1";
				}
				System.out.println(biaoqian);
				// 图片采集
				// 大图格式例子
				// url=http://p3.pstatp.com/w540/11aa00031c961e2e8d0f.webp
				// url=http://pb2.pstatp.com/w540/11aa00031c961e2e8d0f.webp
				// url=http://pb3.pstatp.com/w540/11aa00031c961e2e8d0f.webp
				// 小图格式例子
				// url=http://p3.pstatp.com/w480/11aa00031c961e2e8d0f.webp
				// url=http://pb2.pstatp.com/w480/11aa00031c961e2e8d0f.webp
				// url=http://pb3.pstatp.com/w480/11aa00031c961e2e8d0f.webp
				if (bodyTemp.containsKey("large_image")) {
					JSONObject videoJson = JsonUtil.stringToJson(bodyTemp.getString("large_image"));
					group = "2";
					imageUrl = JsonUtil
							.stringToJson(JsonUtil.stringToJsonArry(videoJson.getString("url_list")).get(0).toString())
							.getString("url");
					width = videoJson.getString("r_width");
					// System.out.println(width);
					height = videoJson.getString("r_height");
					// System.out.println(imageUrl);
				}

				// 视频采集
				// 缩略图
				if (bodyTemp.containsKey("large_cover")) {
					videoImgUrl = JsonUtil.stringToJson(JsonUtil
							.stringToJsonArry(
									JsonUtil.stringToJson(bodyTemp.getString("large_cover")).getString("url_list"))
							.get(0).toString()).getString("url");
					// System.out.println(videoImgUrl);
				}
				// 视频链接
				// http://ic.snssdk.com/neihan/video/playback/?video_id=47bd2bb976fe470090373dbcae8151fa&quality=origin&line=0&is_gif=0
				// http://ic.snssdk.com/neihan/video/playback/?video_id=47bd2bb976fe470090373dbcae8151fa&quality=720p&line=0&is_gif=0
				// http://ic.snssdk.com/neihan/video/playback/?video_id=47bd2bb976fe470090373dbcae8151fa&quality=480p&line=0&is_gif=0
				// http://ic.snssdk.com/neihan/video/playback/?video_id=47bd2bb976fe470090373dbcae8151fa&quality=360p&line=0&is_gif=0
				if (bodyTemp.containsKey("origin_video")) {
					group = "3";
					JSONObject videoJson = JsonUtil.stringToJson(bodyTemp.getString("origin_video"));
					videoUrl = JsonUtil
							.stringToJson(JsonUtil.stringToJsonArry(videoJson.getString("url_list")).get(0).toString())
							.getString("url");
					// System.out.println(videoUrl);
					width = videoJson.getString("width");
					// System.out.println(width);
					height = videoJson.getString("height");
					// System.out.println(height);
				}

				String contentBody = content;
				// 存储采集
				if (!imageUrl.equals("")) {
					contentBody = "<img  src=" + imageUrl + "  width=" + width + " height=" + height + " />";
					// contentBody= "<iframe src=/getRes?url=" + imageUrl + "
					// frameborder=0 scrolling=no width=" + width
					// + " height=" + height + " marginheight=0 marginwidth=0
					// ></iframe>";
				}
				System.out.println(content);

				String sql = "INSERT INTO `mto_posts` VALUES ('" + idstr + "', '" + id + "', " + comment + ", '"
						+ contentBody + "', '" + DateUtil.getDate(max_time + "000", null) + "', 'ueditor', '0', '0', '"
						+ group + "', '0', '0', null, '" + privacy + "', '0', '" + content + "', '" + biaoqian + "', '"
						+ text + "', '8', '" + bury + "', '" + digg + "', '" + share + "');";

				System.out.println(sql);
				try {
					if (ConMysql.insertSql(sql) == 1) {
						System.out.println("插入段子成功：");
					} else {
						System.out.println("插入段子失败：");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (!videoUrl.equals("")) {
					sql = " INSERT INTO `mto_posts_attribute` VALUES ('" + idstr + "', '" + videoImgUrl
							+ "', '" + videoUrl + "','"+height+"','"+width+"');";

					System.out.println(sql);
					try {
						if (ConMysql.insertSql(sql) == 1) {
							System.out.println("插入附件成功：");
						} else {
							System.out.println("插入附件失败：");
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			System.out.println(bodyJs.getBoolean("has_more"));
			if (bodyJs.getBoolean("has_more")) {
				caiPostsInfo(id, max_time);
			}
		} else {
			System.out.println("段子信息不纯在：" + id);
		}
	}
}
